#include "DebugForm.h"
#include "ui_DebugForm.h"
#include <cstdlib>
#include "src/base/Logic.h"
#include "src/define/func_define.h"


QByteArray AscToChar(uchar n)
{
   QByteArray fr;
   fr.append(n);
   return  fr.toHex();
}

DebugForm::DebugForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DebugForm)
{
    ui->setupUi(this);
    Motor::GetInstance()->bufFull.connect([this](std::vector<uchar> vc){
        // std::string r;
        QByteArray fr;
        for(auto v:vc){
            fr.append(AscToChar(v));
            fr.append(" ");
        }
        //ui->textBrowser->append(QString::fromStdString(r));
        ui->textBrowser->append(fr);
    });
}

DebugForm::~DebugForm()
{
    delete ui;
}

void DebugForm::multSportAngle()
{
    bool wise = true;
    if(ui->sp_range->value() < 0){
        wise = false;
    }
    int puls = angleToPuls(abs(ui->sp_range->value()));

    auto gg = MotorFcs::packageMove(wise,puls,200,255);
    MotorFcs::printVect(gg);
    Motor::GetInstance()->sendCMDs(gg);
}

void DebugForm::multSportLen()
{
    bool transe;
    bool wise = true;
    if(ui->sp_rorate->value() < 0){
        wise = false;
    }

    int arg = ui->sp_arg->value();
    int len = ui->sp_rorate->value();

    auto angle = lenToArc(abs(len),arg,transe);
    auto puls =  angleToPuls(angle);
    auto gg = MotorFcs::packageMove(wise,puls,100,255);
    Motor::GetInstance()->sendCMDs(gg);

}

// 手动旋转指定的幅度
void DebugForm::on_pb_motorRorate_clicked()
{
    bool transe;
    bool wise = true;
    if(ui->sp_rorate->value() < 0){
        wise = false;
    }

    int arg = ui->sp_arg->value();
    int len = ui->sp_rorate->value();

    auto angle = lenToArc(abs(len),arg,transe);
    auto t =  angleToPuls(angle);
    auto gg = MotorFcs::packageMove(wise,t,100,255);
    Motor::GetInstance()->sendCMDs(gg);
}

/**
 * @brief DebugForm::on_pb_start_clicked
 * @details 起摆
 */
void DebugForm::on_pb_start_clicked()
{

}

/**
 * @brief DebugForm::on_pb_stop_clicked
 * @details 停摆
 */
void DebugForm::on_pb_stop_clicked()
{


}

void DebugForm::on_pb_send_clicked()
{
    std::string cmds = ui->le_cmd->text().toStdString();
    char split[3] = {' ',',','\t'};
    vector<ulong> values;
    vector<uchar> u_values;
    for(int i=0;i<1;i++)
    {
        values.resize(0);
        values.clear();
        stringstream ss(cmds);
        while(ss.good()){
            string substr;
            getline( ss, substr,split[i]);
            stringstream sd(substr);
            auto value = std::strtoul(("0x"+sd.str()).c_str(), 0, 16);
            // std::cout << ("0x"+sd.str())  << "\t" << value << endl;
            values.push_back(value);
        }
    }

    for(auto it = values.begin();it!=values.end();it++){
        uchar g = static_cast<uchar>(*it);
        u_values.push_back(g);
        std::cout << *it  << "\t" << endl;
    }

    Motor::GetInstance()->sendCMDs(u_values);

    // Motor::GetInstance()->sendCMDs(values);
}

void DebugForm::on_pb_Angle_clicked()
{
    bool wise = true;
    if(ui->sp_range->value() < 0){
        wise = false;
    }
    int puls = angleToPuls(abs(ui->sp_range->value()));

    auto gg = MotorFcs::packageMove(wise,puls,100,255);
    Motor::GetInstance()->sendCMDs(gg);
    MotorFcs::printVect(gg);
}

void DebugForm::on_pb_1p_clicked()
{
    int count = ui->sp_count->value();
    for(int i =0; i<count; i++){
        multSportLen();
    }
}

void DebugForm::on_pb_3p_clicked()
{
    int count = ui->sp_count->value();
    for(int i =0; i<count; i++){
        multSportAngle();
    }
}

void DebugForm::on_pb_aCir_clicked()
{
    int t = angleToPuls(360);
    auto gg = MotorFcs::packageMove(true,t,100,255);
    Motor::GetInstance()->sendCMDs(gg);
}

void DebugForm::on_pb_clear_clicked()
{
    ui->textBrowser->clear();
}
